<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="OrcasThemes">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <title>视频点播系统 - 视频列表</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入 Font Awesome 图标库 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css">
    <!-- 自定义样式 -->
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #4e4e4e;
            color: #333;
            margin: 0;
            padding: 0;
        }

        /* 导航栏样式 */
       .navbar {
            background-color: #2c3e50;
            color: white;
            padding: 10px 20px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

       .search-block {
            background-color: white;
            border-radius: 20px;
            padding: 5px 10px;
            margin-right: 20px;
            display: flex;
            align-items: center;
        }

       .search-block input[type="search"] {
            border: none;
            outline: none;
            width: 200px;
            margin-left: 10px;
        }

       .search-block .fa-search {
            color: #7f8c8d;
        }

        /* 视频列表样式 */
        #home-main {
            padding: 40px;
        }

        #home-main h2.icon {
            color: white;
            font-size: 32px;
            margin-bottom: 20px;
            text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
        }

       .video-grid {
            display: grid;
            grid-template-columns: repeat(3, 1fr);
            grid-gap: 30px;
        }

       .video-card {
            background-color: white;
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            transition: all 0.3s ease;
            position: relative;
        }

       .video-card:hover {
            transform: translateY(-10px);
            box-shadow: 0 12px 20px rgba(0, 0, 0, 0.3);
        }

       .video-thumb {
            position: relative;
            overflow: hidden;
        }

       .video-thumb img {
            width: 100%;
            height: auto;
            transition: transform 0.3s ease;
        }

       .video-thumb:hover img {
            transform: scale(1.1);
        }

       .video-thumb .play-btn {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            color: white;
            font-size: 60px;
            opacity: 0;
            transition: opacity 0.3s ease;
            background-color: rgba(0, 0, 0, 0.5);
            border-radius: 50%;
            padding: 15px;
        }

       .video-thumb:hover .play-btn {
            opacity: 1;
        }

       .video-info {
            padding: 20px;
        }

       .video-info h4 a {
            color: #333;
            text-decoration: none;
            transition: color 0.3s ease;
            font-size: 18px;
        }

       .video-info h4 a:hover {
            color: #3498db;
        }

        /* 上传提示框样式 */
       .upload-prompt {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            height: 100%;
            text-align: center;
            cursor: pointer;
        }

       .upload-prompt i {
            font-size: 48px;
            margin-bottom: 10px;
        }

        /* 模态框样式 */
       .modal-content {
            border-radius: 15px;
            box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
        }

       .custom-modal-header {
            background-color: #2c3e50;
            color: white;
            border-top-left-radius: 15px;
            border-top-right-radius: 15px;
            padding: 20px;
        }

       .modal-body form {
            padding: 20px;
        }

       .modal-body input[type="text"],
       .modal-body input[type="file"] {
            width: 100%;
            padding: 12px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 5px;
            font-size: 16px;
        }

       .subscribe-btn {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 12px 25px;
            border-radius: 5px;
            cursor: pointer;
            transition: background-color 0.3s ease;
            font-size: 16px;
        }

       .subscribe-btn:hover {
            background-color: #2980b9;
        }

        [v-cloak] {
            display: none;
        }
    </style>
</head>

<body>
    <div id="myapp">
        <!-- 导航栏 -->
        <nav class="navbar navbar-expand-lg">
            <div class="container-fluid">
                <div class="search-block">
                    <span class="fa fa-search"></span>
                    <input type="search" id="searchInput" placeholder="搜索">
                    <button id="searchButton" class="btn btn-primary">查询</button>
                </div>
                <!-- 个人中心按钮 -->
                <a href="personal-center.html" class="btn subscribe-btn">个人中心</a>
            </div>
        </nav>

        <!-- 视频列表 -->
        <section id="home-main">
            <h2 class="icon"><i class="fa fa-television" aria-hidden="true"></i>视频列表</h2>
            <div class="video-grid">
                <article class="video-card" v-for="video in videos" v-cloak>
                    <div class="video-thumb">
                        <a class="afterglow post-thumb" :href="`/video.html?id=${video.id}`" target="_blank">
                            <span class="play-btn" title="Play"><i class="fa fa-play-circle headline-round"
                                    aria-hidden="true"></i></span>
                            <img class="img-fluid" :src="video.image" alt="#">
                        </a>
                    </div>
                    <div class="video-info">
                        <h4>
                            <a class="title" href="#">{{video.name}}</a>
                        </h4>
                        <div class="d-flex justify-content-end">
                            <!-- 新增收藏图标 -->
                            <i class="fa fa-bookmark" @click="collectVideo(video)" :disabled="video.isCollecting"
                                style="cursor: pointer; margin-right: 10px;"></i>
                            <i class="fa fa-thumbs-up" @click="likeVideo(video)" :disabled="video.isLiking"
                                style="cursor: pointer;"></i>
                        </div>
                    </div>
                </article>
                <!-- 上传视频提示框 -->
                <article class="video-card">
                    <div class="upload-prompt" @click="openUploadModal">
                        <i class="fa fa-video"></i>
                        <p>上传视频</p>
                    </div>
                </article>
            </div>
        </section>

        <!-- 模态框 -->
        <div class="modal fade" id="enquirypopup" tabindex="-1" aria-labelledby="enquirypopupLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header custom-modal-header">
                        <h2 class="icon"><i class="fa fa-television" aria-hidden="true"></i>上传视频</h2>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <form name="info_form" class="form-inline" action="/video" method="post"
                            enctype="multipart/form-data" @submit.prevent="submitForm">
                            <div class="mb-3">
                                <label for="videoName" class="form-label">输入视频名称</label>
                                <input type="text" class="form-control" id="videoName" name="name">
                            </div>
                            <div class="mb-3">
                                <label for="videoInfo" class="form-label">请输入视频简介</label>
                                <input type="text" class="form-control" id="videoInfo" name="info">
                            </div>
                            <div class="mb-3">
                                <label for="videoFile" class="form-label">提交视频</label>
                                <input type="file" class="form-control" id="videoFile" name="video">
                            </div>
                            <div class="mb-3">
                                <label for="coverImage" class="form-label">提交封面</label>
                                <input type="file" class="form-control" id="coverImage" name="image">
                            </div>
                            <div class="text-end">
                                <button class="subscribe-btn" type="submit" title="Subscribe">提交上传</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入 jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 引入 Bootstrap JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入 Vue.js -->
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>
        let app = new Vue({
            el: '#myapp',
            data: {
                author: "ZhangShuo",
                videos: []
            },
            methods: {
                get_allvideos: function () {
                    $.ajax({
                        url: "/video",
                        type: "get",
                        context: this,
                        success: function (result, status, xhr) {
                            result.forEach(video => {
                                video.isLiking = false; // 新增属性，用于标记是否正在点赞
                                video.isCollecting = false; // 新增属性，用于标记是否正在收藏
                            });
                            this.videos = result;
                        },
                        error: function (error) {
                            console.error('获取视频数据失败:', error);
                        }
                    });
                },
                openUploadModal: function () {
                    $('#enquirypopup').modal('show');
                },
                searchVideos: function () {
                    let searchValue = $('#searchInput').val().toLowerCase();
                    if (searchValue) {
                        this.videos = this.videos.filter(video => {
                            return video.name.toLowerCase().includes(searchValue);
                        });
                    } else {
                        this.get_allvideos(); // 若搜索框为空，重新获取所有视频
                    }
                },
                submitForm: function () {
                    const form = document.forms.info_form;
                    const formData = new FormData(form);
                    $.ajax({
                        url: form.action,
                        type: form.method,
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function () {
                            window.location.href = 'list.html';
                        },
                        error: function () {
                            console.error('上传失败');
                        }
                    });
                },
                likeVideo: function (video) {
                    if (video.isLiking) return; // 如果正在点赞，直接返回
                    video.isLiking = true; // 标记为正在点赞
                    $.ajax({
                        url: `/video/${video.id}/like`,
                        type: "post",
                        success: function () {
                            alert('点赞成功！');
                            console.log('点赞成功');
                        },
                        error: function () {
                            console.error('点赞失败');
                            alert('点赞失败，请稍后再试'); // 给用户友好提示
                        },
                        complete: function () {
                            video.isLiking = false; // 无论成功失败，都标记为点赞结束
                        }
                    });
                },
                collectVideo: function (video) {
                    if (video.isCollecting) return; // 如果正在收藏，直接返回
                    video.isCollecting = true; // 标记为正在收藏
                    $.ajax({
                        url: `/video/${video.id}/collect`,
                        type: "post",
                        success: function () {
                            alert('收藏成功！');
                            console.log('收藏成功');
                        },
                        error: function () {
                            console.error('收藏失败');
                            alert('收藏失败，请稍后再试'); // 给用户友好提示
                        },
                        complete: function () {
                            video.isCollecting = false; // 无论成功失败，都标记为收藏结束
                        }
                    });
                }
            },
            mounted() {
                this.get_allvideos();
                $('#searchButton').on('click', this.searchVideos);
            }
        });
    </script>
</body>

</html>